package my.test;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.go2.shipping.common.DateUtils;
import com.go2.shipping.common.StringUtil;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.net.URLEncoder;
import java.util.*;

public class Otest {


    public static void sss() throws UnsupportedEncodingException {
        Map<String, Object> data = new HashMap<String, Object>();
        data.put("cp_code", "STO");
        data.put("need_encrypt", true);
        //发件人信息-sender
        String consignorName = "于航";
        String consignorPhone = "15803097965";
        Map<String, Object> sender = new HashMap<String, Object>();
        sender.put("name", consignorName);
        sender.put("mobile", consignorPhone);
        Map<String, Object> address = new HashMap<String, Object>();
        address.put("province", "浙江省");
        address.put("city", "湖州市");
        address.put("district", "吴兴区");
        address.put("detail", "织里镇中华西路888号");
        sender.put("address", address);
        data.put("sender", sender);

        //请求面单信息-start
        List<Map<String,Object>> tradeInfoList = new ArrayList<Map<String,Object>>();
        Map<String, Object> tradeInfo = new HashMap<String, Object>();
        tradeInfo.put("object_id", UUID.randomUUID().toString());
        tradeInfo.put("user_id", String.valueOf(55934));
        tradeInfo.put("template_url", "http://cloudprint.cainiao.com/template/standard/201/214");

        //包裹信息-商品
        Map<String, Object> packageInfo = new HashMap<String, Object>();
        List<Map<String, Object>> packageItems = new ArrayList<Map<String,Object>>();

        Map<String, Object> packageItem = new HashMap<String, Object>();
        packageItem.put("name", "衬衣");
        packageItem.put("count", 2);
        packageItems.add(packageItem);
        packageInfo.put("items", packageItems);
        tradeInfo.put("package_info", packageInfo);
        //发件人信息
        Map<String, Object> recipient = new HashMap<String, Object>();
        recipient.put("name", "王天");
        recipient.put("mobile", "15804987854");
        Map<String, Object> recipientAddress = new HashMap<String, Object>();

        recipientAddress.put("province", "重庆");
        recipientAddress.put("detail", "凤鸣镇");
        recipientAddress.put("city", "重庆");
        recipientAddress.put("district", "云阳");
        recipient.put("address", recipientAddress);
        tradeInfo.put("recipient", recipient);
        //订单信息
        Map<String, Object> orderInfo = new HashMap<String, Object>();
        String channelType = "TB";
        orderInfo.put("order_channels_type", channelType);

        String orderNo = "20210232908321312";

        orderInfo.put("trade_order_list", new String[]{orderNo});
        tradeInfo.put("order_info", orderInfo);

        tradeInfoList.add(tradeInfo);
        data.put("trade_order_info_dtos", tradeInfoList);

        List<NameValuePair> params = new ArrayList<NameValuePair>();

        System.out.println(JSON.toJSONString(data));
    }


    private static List<Map<String, Object>> getValidAddress(String token,long shopId,String shopName)  {
        HttpClient client = HttpClients.createDefault();
        HttpGet httpRequst = new HttpGet("http://eapi.ximgs.net:30001/taobaogsb/get-wlb-search"+"?token=" + token);
        List<Map<String, Object>> waybillList = new ArrayList<Map<String,Object>>();
        HttpResponse res;
        try {
            res = client.execute(httpRequst);
            String result = "";
            HttpEntity entity = res.getEntity();
            result = EntityUtils.toString(entity);
            Map<String, Object> data = (Map<String, Object>) JSONObject.parseObject(result, HashMap.class).get("data");
            if(!data.containsKey("waybill_apply_subscription_cols")){
//                throw new TokenException("授权信息验证失败!");
            }
            Map<String, Object> cols = (Map<String, Object>) data.get("waybill_apply_subscription_cols");

            if(!cols.containsKey("waybill_apply_subscription_info")){
                return null;
            }

            List<Map<String, Object>> infos = (List<Map<String, Object>>) cols.get("waybill_apply_subscription_info");
            for(Map<String, Object> info : infos){
                Map<String, Object> accountCols = (Map<String, Object>) info.get("branch_account_cols");
                List<Map<String, Object>> accounts = (List<Map<String, Object>>) accountCols.get("waybill_branch_account");
                for(Map<String, Object> account : accounts){
                    Map<String, Object> addressCols = (Map<String, Object>) account.get("shipp_address_cols");
                    List<Map<String, Object>> addressList = (List<Map<String, Object>>) addressCols.get("address_dto");
                    Map<String, Object> waybill = new HashMap<String, Object>();
                    waybill.put("shopId", shopId);
                    waybill.put("shopName", shopName);
                    waybill.put("cpCode", info.get("cp_code").toString());//物流服务商ID
                    waybill.put("cpName", cpMap.get(info.get("cp_code").toString()));
                    waybill.put("name", account.get("branch_name"));//网点名称
                    waybill.put("code", account.get("branch_code"));
                    waybill.put("quantity", account.get("quantity"));//可用面单数量
                    waybill.put("allocatedQuantity", account.get("allocated_quantity"));//已用面单数量
                    waybill.put("cancelQuantity", account.get("cancel_quantity"));//取消的面对总数
//					for(Map<String, Object> address : addressList){
//						waybill.putAll(address);
//						waybillList.add(waybill);
//					}
                    if(addressList!= null && addressList.size() > 0){
                        waybill.putAll(addressList.get(0));
                    }
                    waybillList.add(waybill);
                }
            }
            return waybillList;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static final Map<String, String> cpMap = new HashMap<String, String>(){{
        put("SF", "顺丰");
        put("EMS", "EMS标准快递");
        put("EYB", "EMS经济快件");
        put("ZJS", "宅急送");
        put("YTO", "圆通");
        put("ZTO", "中通");
        put("HTKY", "百世汇通");
        put("UC", "优速");
        put("STO", "申通");
        put("TTKDEX", "天天快递");
        put("QFKD", "全峰");
        put("FAST", "快捷");
        put("POSTB", "邮政小包");
        put("GTO", "国通");
        put("YUNDA", "韵达");
        put("DBKD", "德邦快递");
    }};

    public static void main(String[] args) throws UnsupportedEncodingException {



//      Otest o = new Otest();

        getValidAddress("6100c10edfc5adbef2155bcd3918b3edfabc52aee42901c24226209",1,"aa");

    }

    public static float randomFloat(float min,float max)
    {
        Random random = new Random();
        float f= random.nextFloat()*(max-min)+min;
        return f;
    }
}
